﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter24.Examples
{
    using System.Xml.Linq;

    public class Code12
    {
        public static void Begin()
        {
            var xmlFileName = @".\customer-orders.xml";
            var customers = XDocument.Load(xmlFileName);
            Console.WriteLine("All documnet:");
            var queryResult =
                from addresses in customers.Descendants()
                select addresses.Name;
            foreach (var item in queryResult)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("Press Enter/Return to contiune:");
            Console.ReadLine();
        }

        public static void Begin2()
        {
            var xmlFileName = @".\customer-orders.xml";
            var customers = XDocument.Load(xmlFileName);
            Console.WriteLine("Descendants named 'address':");
            var queryResult =
                from address in customers.Descendants()
                select address.Name;
            foreach (var item in queryResult.Distinct())
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("Press Enter/Return to contiune:");
            Console.ReadLine();
        }

        public static void Begin3()
        {
            var xmlFileName = @".\customer-orders.xml";
            var customers = XDocument.Load(xmlFileName);
            Console.WriteLine("Descendants named 'address':");
            var queryResult =
                from address in customers.Descendants("address")
                select address.Name;
            foreach (var item in queryResult)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("Press Enter/Return to contiune:");
            Console.ReadLine();
        }

        public static void Begin4()
        {
            var xmlFileName = @".\customer-orders.xml";
            var customers = XDocument.Load(xmlFileName);
            Console.WriteLine("Attritues of descendants named 'address':");
            var queryResult =
                from address in customers.Descendants("address").Attributes()
                select address.Name;
            foreach (var item in queryResult)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("Press Enter/Return to contiune:");
            Console.ReadLine();
        }

        public static void Begin5()
        {
            var xmlFileName = @".\customer-orders.xml";
            var customers = XDocument.Load(xmlFileName);
            Console.WriteLine("address attributes named 'Line1':");
            var queryResult =
                from line1 in customers.Descendants("address").Attributes("Line1")
                select line1;
            foreach (var item in queryResult)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("Press Enter/Return to contiune:");
            Console.ReadLine();
        }

        public static void Begin6()
        {
            var xmlFileName = @".\customer-orders.xml";
            var customers = XDocument.Load(xmlFileName);
            Console.WriteLine("address attributes named 'ID':");
            var queryResult =
                from id in customers.Descendants("address").Attributes("ID")
                select id;
            foreach (var item in queryResult)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("Press Enter/Return to contiune:");
            Console.ReadLine();
        }

        public static void Begin7()
        {
            var xmlFileName = @".\customer-orders.xml";
            var customers = XDocument.Load(xmlFileName);
            Console.WriteLine("address attributes named 'Line1' value:");
            var queryResult =
                from line1 in customers.Descendants("address").Attributes("Line1")
                select line1.Value;
            foreach (var item in queryResult)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("Press Enter/Return to contiune:");
            Console.ReadLine();
        }

        public static void Begin8()
        {
            var xmlFileName = @".\customer-orders.xml";
            var customers = XDocument.Load(xmlFileName);
            Console.WriteLine("address attributes named 'Line1' value:");
            var queryResult =
                from line1 in customers.Descendants("address").Attributes("Line1")
                select line1.Value;
            Console.WriteLine("Line1 Min(): {0}", queryResult.Min());
            Console.WriteLine("Press Enter/Return to contiune:");
            Console.ReadLine();
        }
    }
}
